package com.cloudpivot.receivales.excel.controller;

import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.excel.domain.ExcelForBJDTO;
import com.cloudpivot.receivales.excel.service.ProcessExcelService;
import com.cloudpivot.receivales.excel.view.RspEnumForExcel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.*;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * excel 处理层
 */

@Controller
@Slf4j
@RequestMapping("/ProcessExcel")
public class ProcessExcelController {
    @Autowired
    private ProcessExcelService processExcelService;

    /**
     * @param reqJson {base64 : ""}
     * @return code +msg +data(存放list)
     */
    @ResponseBody
    @PostMapping("/baojiaExcelImport")
    public Map<String, Object> baojiaExcelProcess(@RequestBody JSONObject reqJson) {
        Map<String, Object> rsp = new HashMap<>();
        if (StringUtils.isBlank(reqJson.getString("refId"))) {
            rsp.put("code", -99);
            rsp.put("msg", RspEnumForExcel.getValue(-99));
            rsp.put("data", null);
            return rsp;
        }
        String refId = reqJson.getString("refId");
        String base64 = processExcelService.getFileBase64(refId);
        InputStream inputStream = base64ToInputStream(base64);
        rsp = processExcelService.importExcelForBJ(inputStream);
        return rsp;
    }

    /**
     * 解析 400订货流程 excel
     * @param reqJson
     * @return
     */
    @ResponseBody
    @PostMapping("/preSaleExcelImport")
    public Map<String, Object> preSaleExcelProcess(@RequestBody JSONObject reqJson) {
        Map<String, Object> rsp = new HashMap<>();
        if (StringUtils.isBlank(reqJson.getString("refId"))) {
            rsp.put("code", -99);
            rsp.put("msg", RspEnumForExcel.getValue(-99));
            rsp.put("data", null);
            return rsp;
        }
        String refId = reqJson.getString("refId");
        String base64 = processExcelService.getFileBase64(refId);
        InputStream inputStream = base64ToInputStream(base64);
        rsp = processExcelService.importExcelForPresale(inputStream);
        return rsp;
    }


    /**
     * inputstream 转 base64
     *
     * @param path
     * @return
     */
    private String inputStreamToBase64(String path) {
        try {
            File file = new File(path);
            FileInputStream inputStream = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputStream.read(buffer);
            inputStream.close();
            Base64.Encoder encoder = Base64.getEncoder();
            String base64 = encoder.encodeToString(buffer);
            return base64;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * base64 转 inputstream
     *
     * @param base64
     * @return
     */
    private InputStream base64ToInputStream(String base64) {
        byte[] bytes = Base64.getDecoder().decode(base64);
        InputStream inputStream = new ByteArrayInputStream(bytes);
        return inputStream;
    }
}
